package com.declum.archon.client;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class BaseRequest implements IRequest {
	Socket		socket;
	private int	TIMEOUT	= 3000;

	public void connect(InetSocketAddress address) throws IOException {
		this.socket = new Socket();
		this.socket.connect(address, this.TIMEOUT);
	}

	public void connect(InetAddress address, int port) throws IOException {
		this.connect(new InetSocketAddress(address, port));
	}

	public void connect(String address, int port) throws IOException {
		this.connect(InetAddress.getByName(address), port);
	}

	public void connect(String address) throws IOException {
		String[] parts = address.split(":");
		if (parts.length != 2) {
			throw new IOException("Invalid address");
		}
		String host = parts[0];
		int port = Integer.parseInt(parts[1]);
		this.connect(InetAddress.getByName(host), port);
	}

	protected String getHeaderSring(String header, String value) {
		return header + ": " + value + "\r\n";
	}

	protected String getHeaderSring(String header, String[] values) {
		String headerString = header + ": ";
		for (String value : values) {
			headerString += value + " ";
		}
		headerString += "\r\n";
		return headerString;
	}
}
